/**create by liuhua at 2019年6月13日 上午9:49:49**/
package com.star.truffle.core.mybatis.handler;

import java.sql.CallableStatement;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Map;

import org.apache.ibatis.type.BaseTypeHandler;
import org.apache.ibatis.type.JdbcType;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.star.truffle.core.jackson.StarJson;

public class MapForObjectHandler extends BaseTypeHandler<Map<String, Object>> {

  private StarJson starJson = new StarJson(new ObjectMapper());
  
  @Override
  public void setNonNullParameter(PreparedStatement ps, int i, Map<String, Object> parameter, JdbcType jdbcType) throws SQLException {
    ps.setString(i, starJson.obj2string(parameter));
  }

  @Override
  public Map<String, Object> getNullableResult(ResultSet rs, String columnName) throws SQLException {
    return starJson.str2Map(rs.getString(columnName));
  }

  @Override
  public Map<String, Object> getNullableResult(ResultSet rs, int columnIndex) throws SQLException {
    return starJson.str2Map(rs.getString(columnIndex));
  }

  @Override
  public Map<String, Object> getNullableResult(CallableStatement cs, int columnIndex) throws SQLException {
    return starJson.str2Map(cs.getString(columnIndex));
  }

}
